package com.unistec.fileserver.server.provider;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.csource.common.NameValuePair;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.unistec.fileserver.client.entity.Resource;
import com.unistec.fileserver.server.enums.FileType;
import com.unistec.framework.utils.Objects;

/**
 * 存储提供者
 * 
 * @description
 * @author 胡晓光
 * @date 2021-9-29
 * @time 9:15:36
 *
 */
public interface IStorageProvider {
	/**
	 * 上传文件
	 * 
	 * @description
	 * @author 胡晓光
	 * @date 2021-9-29
	 * @time 9:19:39
	 * 
	 * @param paramHolder
	 * @param folder
	 * @param f
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	Resource uploadFile(JSONObject paramHolder, String folder, File f, String fileName) throws Exception;

	/**
	 * 上传文件
	 * 
	 * @description
	 * @author 胡晓光
	 * @date 2021-9-29
	 * @time 12:58:51
	 * 
	 * @param paramHolder
	 * @param f
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	Resource uploadFile(JSONObject paramHolder, File f, String fileName) throws Exception;

	/**
	 * 上传文件
	 * 
	 * @description
	 * @author 胡晓光
	 * @date 2021-9-29
	 * @time 12:58:51
	 * 
	 * @param paramHolder
	 * @param f
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	Resource uploadFile(JSONObject paramHolder, File f) throws Exception;

	/**
	 * 上传文件
	 * 
	 * @description
	 * @author 胡晓光
	 * @date 2021-9-29
	 * @time 12:58:51
	 * 
	 * @param paramHolder
	 * @param f
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	Resource uploadFile(JSONObject paramHolder, MultipartFile file) throws Exception;

	/**
	 * 上传文件
	 * 
	 * @description
	 * @author 胡晓光
	 * @date 2021-9-29
	 * @time 12:58:51
	 * 
	 * @param paramHolder
	 * @param f
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	Resource uploadFile(JSONObject paramHolder, String folder, File f) throws Exception;

	/**
	 * 上传文件
	 * 
	 * @description
	 * @author 胡晓光
	 * @date 2021-9-29
	 * @time 12:58:51
	 * 
	 * @param paramHolder
	 * @param f
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	Resource uploadFile(JSONObject paramHolder, String folder, MultipartFile file) throws Exception;

	/**
	 * 上传文件
	 * 
	 * @description
	 * @author 胡晓光
	 * @date 2021-9-29
	 * @time 12:58:51
	 * 
	 * @param paramHolder
	 * @param f
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	Resource uploadFile(JSONObject paramHolder, String filename, byte[] data) throws Exception;

	/**
	 * 上传文件
	 * 
	 * @description
	 * @author 胡晓光
	 * @date 2021-9-29
	 * @time 12:58:51
	 * 
	 * @param paramHolder
	 * @param f
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	Resource uploadFile(JSONObject paramHolder, String folder, String filename, byte[] data) throws Exception;

	/**
	 * 上传文件
	 * 
	 * @description
	 * @author 胡晓光
	 * @date 2021-9-29
	 * @time 12:58:51
	 * 
	 * @param paramHolder
	 * @param f
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	Resource uploadFile(JSONObject paramHolder, String folder, String filename, InputStream in) throws Exception;

	/**
	 * 删除文件
	 * 
	 * @description
	 * @author 胡晓光
	 * @date 2021-9-29
	 * @time 12:58:51
	 * 
	 * @param paramHolder
	 * @param f
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	boolean deleteFile(JSONObject paramHolder, String url) throws Exception;

	/**
	 * 判断文件是否存在
	 * 
	 * @description
	 * @author 胡晓光
	 * @date 2021-9-29
	 * @time 12:59:18
	 * 
	 * @param paramHolder
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	boolean existFile(JSONObject paramHolder, String fileName) throws Exception;

	/**
	 * 检查存储配额是否超出
	 * 
	 * @param params
	 * @return
	 */
	boolean exceedQuota(JSONObject paramHolder, JSONObject params);

	/**
	 * 获取文件上传支持的类型
	 * 
	 * @author 韩凯
	 * @date 2020年6月11日 下午 05:04
	 */
	List<FileType> getFileTypes();

	default NameValuePair[] jsonObjectNameValuePair(JSONObject params) {
		List<NameValuePair> pairs = new ArrayList<>();
		Iterator<String> iter = params.keySet().iterator();
		while (iter.hasNext()) {
			String key = iter.next();
			Object value = params.get(key);
			if (Objects.nonNull(value)) {
				pairs.add(new NameValuePair(key, value.toString()));
			}
		}
		return pairs.toArray(new NameValuePair[pairs.size()]);
	}
}
